﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using LY.Common;
using LY.Entity;
using LY.Entity.Jsons;
using LY.Interface;

namespace QJ.CMS.Areas.Admin.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        private ISystemService _sservice;
        public LoginController(ISystemService sService)
        {
            _sservice = sService;
        }

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult GetValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(5);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }

        // GET: /Login/
        public JsonResult Singin(string Name, string Password, string code, FormCollection form)
        {

            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                string errmsg = "";
                if (!Validate(Name, Password, code, out errmsg))
                {
                    rMsg.Status = -1;
                    rMsg.Message = errmsg;
                    return Json(rMsg);
                }

                //验证码是否相等
                if (Session["ValidateCode"].ToString() != code)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "验证码输入有误！";
                    return Json(rMsg);
                }

                //验证用户名、密码
                //string pwd = MD5Util.GetMD5_32(Password);
                string pwd = Password;


                S_User user = null;
                user = _sservice.LoginUser(Name, pwd);

                //Member_EX member = _lservice.LoginUser(Name, pwd);
                if (user != null)
                {
                    rMsg.Status = 1;
                    System.Web.HttpContext.Current.Session["LoginUseInfo"] = user;
                    FormsAuthentication.SetAuthCookie(user.ID.ToString(), false);

                    //if (System.Web.HttpContext.Current.Session["ReturnUrl"] == null)
                    //{
                    //    rMsg.Data = "/Home/Index";
                    //}
                    //else
                    //{
                    //    rMsg.Data = System.Web.HttpContext.Current.Session["ReturnUrl"].ToString();
                    //}
                    rMsg.Data = "/Admin/Home";
                    //Response.Redirect("/Home/Index.html");

                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "用户名或密码有误！";
                }

            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        /// <summary>
        /// 数据格式验证
        /// </summary>
        /// <param name="member"></param>
        /// <param name="errmsg"></param>
        /// <returns></returns>
        private static bool Validate(string Name, string Password, string code, out string errmsg)
        {
            errmsg = "";
            if (Name == null)
            {
                errmsg = "参数错误！";
                return false;
            }
            if (string.IsNullOrEmpty(Name))//用户名
            {
                errmsg = "请您输入登录用户名！";
                return false;
            }
            if (string.IsNullOrEmpty(Password))//密码
            {
                errmsg = "请您输入登录密码！";
                return false;

            }
            //if (Session["ValidateCode"].ToString()!=code)//验证码
            //{
            //    errmsg = "验证码错误!";
            //    return false;
            //}
            return true;

        }

        //退出
        [HttpPost]
        public JsonResult Exit()
        {
            Response.Cookies.Clear();
            FormsAuthentication.SignOut();
            Session.Clear();
            JsonSMsg rMsg = new JsonSMsg();
            return Json(rMsg);
        }
    }
}
